<?php
/**
 *  class for replacing components
 *
 * @package code_com
 * @version 1.0
 * @copyright Copyright Urs Lang
 * @license BSD License (http://www.opensource.org/licenses/bsd-license)
 */




/*<div><a href="#" onclick="showProperty(); return false;">edit</a> delete
<div id="mapcontainer" style="width:px; height:px; border:2px inset #ccc"></div>
</div>*/






class com_object
    {
    public $com;
    public $comPath;
    }

class code_com
    {

    // gibt <a ein problem????? (bei event manuell drin)    
    // bei <a gibts das Problem, dass innerhalb eines Linkes ebenfalls eine Tabelle erstellt wird (z.B. href="data_delete")

    static $htmlTag      =
               "ul,ol,li,table,tbody,tr,td,div,h1,h2,h3,h4,label,p,span,data,snapol,select,input,textarea,img,auto_refresh,document";
    private $htmlElement =
                "ul,li,ol,table,tbody,tr,td,div,h1,h2,h3,h4,label,p,span,data,if,snapol,select,input,textarea,img,auto_refresh,document";

    public $header       = "";
    public $jsHeader     = array();
    public $cssHeader    = array();

    public $pageTitle    = "";
    public $pageUrl      = "";
    public $pageTemplate = "";
    public $pageId       = 0;

    // public $dom;
    //public $dataCom=new data();

    public $queryCounter = 0;
    public $phpScriptCounter = 0;
    public $jsScriptCounter = 0;

    public $compileTable = "template";
    public $compileField = "template2";
    
    // Pfad für Inclusions  Css, Php und Js Inlclusion Files
    public $preIncludePath = "";    
    
    public $compileId    = 0;

    private $arrayCom    = array();
    private $arrayComAttribute = array();

    //private $developerModus;
    private $resetCounter = 0;              
    
   
    

    function __construct()
        {
        $timer               =new timer("Com Constructor");

        //global $developerModus;
        //$this->developerModus=$developerModus;

        if (config::$developerModus)
            {

            // Componetents aus Verezichnis auslesen
            $folderCom=new folder();
            $folderCom->getFolder("/com", true);

            //$this->arrayCom=array();

            do
                {
                $row=array();

                $row['com']      =$folderCom->data['filename'];
                $row['com_path'] =$folderCom->data['dir'];
                $this->arrayCom[]=$row;

                // falls Attribute
                //if ()

                } while ($folderCom->nextRow());


            // Com Attribute einlesen
            $folderCom=new folder();
            $folderCom->getFolder("/com_attribute", true);

            //$this->arrayCom=array();

            do
                {
                $row=array();

                $row['com']               =$folderCom->data['filename'];
                $row['com_path']          =$folderCom->data['dir'];
                $this->arrayComAttribute[]=$row;
                //print_r($row);       
                } while ($folderCom->nextRow());
            }
        else
            {
            //$dataCom=new data();
            //$dataCom->getTable("_com", "com_type<>'attribute'", "com");

            foreach (data::getTable("_com", "com_type<>'attribute'", "com") as $data)
            //do
                {
                $row=array();

                $row['com']      = $data['com'];  //  $dataCom->data['com'];
                $row['com_path'] ="";
                $this->arrayCom[]=$row;
                } //while ($dataCom->nextRow());
            }

        //$dataCom=new data();
        //$dataCom->getTable("_com", "com_type='attribute'", "com");
        foreach (data::getTable("_com", "com_type='attribute'", "com") as $data)
        
        
        
        //do
            {
            $row=array();

            $row['com']               = $data['com'];   //$dataCom->data['com'];
            $row['com_path']          ="";
            $this->arrayComAttribute[]=$row;
            } //while ($dataCom->nextRow());

        $timer->stopTimer();
        }

    function replaceCom($html)
        {

            
        //echo $this->pageUrl."<br>";
            
        // Inlcusion Pfad angeben            
        $count            =substr_count($this->pageUrl, '/') - 1;
        $this->preIncludePath = "";
        for ($n=0; $n < $count; $n++)
            $this->preIncludePath=$this->preIncludePath . "../";

            
            //echo $this->preIncludePath;
            
            
        // Werte zur?cksetzen  (m?ssen ausserhalb aufgerufen werden, da diese funktion inkremental aufgerufen werden kann)
        /*$this->header = "";            
        $this->queryCounter = 0;
        $this->phpScriptCounter = 0;
        $this->jsScriptCounter = 0;   */
        /*//echo $html;


        //$this->dom = $dom2;

        /* falls kein Formular Element vorhanden ist
        $dom=str_get_html($html);
        
        
        
        // falls kein form element
        $tagArray=$dom->find("input");
   
            if (sizeof($tagArray) > 0)
                {

                foreach ($tagArray as $e)
                    {
                        //if (isset($e->com)) {
                        if (isset($e->type)) {
                            if ($e->type == "submit") {

                            //$com = $e->com;  
                            $com = "form_save";  
                            
                            $table = "";
                            if (isset($e->table)) 
                                $table = $e->table;
                            
                            $redirect = "";
                            if (isset($e->redirect)) 
                                $redirect = $e->redirect;
                            
                            
                            //$e->com = null;
                            
                            $html=$dom->save();
                            $dom->clear();
                            unset($dom);
                            
                            
                            $html = '<form action="'.$com.'" table="'.$table.'" redirect="'.$redirect.'">'.$html.'</form>';
                            
                            }
                        }
                    }
                }  */

        $dom=str_get_html($html);

        //$timer=new timer_com("replace Tag");

        $timer=new timer_com("replace Form");
        $dom=$this->replaceForm($dom);
        $timer->stopTimer();

        // umwandlung
        $dom=$this->resetDom($dom);

        
        
        
            $timer=new timer_com("reset DOM TEST");
    
    for ($n=0; $n<20; $n++) {
        
         $dom=$this->resetDom($dom);  
    }
    
    
    
             $timer->stopTimer();    
    
    
        // problem for (bei label)

        // muss mehrmals ausgef?hrt werden (f?r nested data elemente)
        $timer=new timer_com("replace Attribute");
        $dom=$this->replaceAttribute($dom);
        $timer->stopTimer();

        // umwandlung
        $dom=$this->resetDom($dom);

        // muss mehrmals ausgef?hrt werden (f?r nested data elemente)
        //$timer=new timer_com("replace Attribute");
        $dom=$this->replaceAttribute($dom);
        //$timer->stopTimer(); 

        // umwandlung
        $dom=$this->resetDom($dom);

        //$timer=new timer_com("replace Attribute");
        $dom=$this->replaceAttribute($dom);
        //$timer->stopTimer();      
        $dom=$this->resetDom($dom);

        $timer=new timer_com("replace Com Attribute");
        $dom=$this->replaceComAttribute($dom);
        $timer->stopTimer();
        $dom=$this->resetDom($dom);

        $timer=new timer_com("replace Com Attribute");
        $dom=$this->replaceComAttribute($dom);
        $timer->stopTimer();

        $dom=$this->resetDom($dom);

        $timer=new timer_com("replace Com Attribute");
        $dom=$this->replaceComAttribute($dom);
        $timer->stopTimer();

        $dom=$this->resetDom($dom);

        $timer=new timer_com("replace Form");
        $dom=$this->replaceForm($dom);
        $timer->stopTimer();

        // umwandlung
        $dom=$this->resetDom($dom);


        // muss mehrmals ausgef?hrt werden (f?r nested data elemente)
        // oder f?r ersetzungen in com's
        $timer=new timer_com("replace Attribute");
        $dom=$this->replaceAttribute($dom);
        $timer->stopTimer();

        // umwandlung
        $dom=$this->resetDom($dom);

        // muss mehrmals ausgef?hrt werden (f?r nested data elemente)
        $timer=new timer_com("replace Attribute");
        $dom=$this->replaceAttribute($dom);
        $timer->stopTimer();

        // umwandlung
        $dom=$this->resetDom($dom);

        //$timer=new timer_com("replace href");
        $dom=$this->replaceHref($dom);
        //$timer->stopTimer();

        $dom=$this->resetDom($dom);

        $timer=new timer_com("replace event");
        $dom=$this->replaceEvent($dom);
        $timer->stopTimer();

        $html=$dom->save();
        $dom->clear();
        unset($dom);

        $html=$this->replacePlaceholder($html);

        $timer=new timer_com("replace short bracket");
        $html =$this->replaceShortBracket($html, "short_bracket");
        $timer->stopTimer();

        /*$timer=new timer_com("replace bracket");
        
        // f?r welche befehle brauchts das????
        $html =$this->replaceBracket($html);
        $timer->stopTimer();*/

        //$timer=new timer_com("replace placeholder2");
        //$html =$this->replacePlaceholder($html);
        //$timer->stopTimer();

        //$timer=new timer_com("dom-html");

        //for ($n=0; $n<100; $n++) {
        /*    f?r was war das????
        
        $dom  =str_get_html($html);
    
            $dom  =$this->replaceAttribute($dom);
    
            $html =$dom->save();
            $dom->clear();
            unset($dom);      */
        //}
        //$timer->stopTimer();


        //$timer->stopTimer();


        // JsHeader und CssHeader einf?gen

        $tmp="";

        
        //print_r($this->jsHeader);
        
        if (sizeof($this->jsHeader) > 0)
            {
            for ($n=0; $n < sizeof($this->jsHeader); $n++)
                {

                
                $jsFilename = $this->jsHeader[$n];
                
                // erstes Slash abschneiden   
                if ($jsFilename[0] == "/")
                    $jsFilename=substr($jsFilename, 1);

                $tmp=$tmp . '<script src="' . $this->preIncludePath . $jsFilename . '"></script>';
                $tmp = $tmp."\r\n";
                }                             

            $this->header=$tmp . $this->header;
            }


        // eMail Adressen kodieren
        $html=$this->replaceMailAdress($html);
        
        
        
        
       // $html = str_ireplace("[pre_inclusion_url]", $this->preIncludePath, $html);
       // $this->header = str_ireplace("[pre_inclusion_url]", $this->preIncludePath, $this->header);
          
        

        return $html;
        }

    private function resetDom($dom)
        {
      // wieder aktivieren
       $timer=new timer_com("resetDom");


        // umwandlung
        $html =$dom->save();
        $dom->clear();
        unset($dom);
        $dom =str_get_html($html);

        //echo "Reset Counter ".$this->resetCounter." --- ".$html;
        //echo "-----------------------";

    /* warum 2. mal
        $html=$dom->save();
        $dom->clear();
        unset($dom);
        $dom=str_get_html($html);  */
        // echo "Reset Counter ".$this->resetCounter." --- ".$html;


        //  echo "\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r";
        $this->resetCounter++;

        $timer->stopTimer();   

        return $dom;
        }


    // replace com attribute (i.e. <div com="mp3" url="">MP3</div>)
    private function replaceComAttribute($dom)
        {
        $tagArray=$dom->find("[com]");

        foreach ($tagArray as $e)
                    {
                    
                        $com['com'] = strtolower($e->com);
                        
                        //if (strtolower($e->com) == strtolower($com['com']))
                            //{
                                
                               // echo $com['com'];
                                
                            api::checkRquiredApi($com['com']);
                            // muss vor outer replace gel?scht werden
                            $e->com=null;


                            //echo "replace {$e->com}";

                            // falls data_var noch nicht gesetzt wurde
                            if (!isset($e->data_var))
                                $e->data_var=$this->queryCounter;


                            // Header einf?gen
                            // --> Problem doppelte eintr?ge vermeiden  
                            $timer=new timer_com("setComHeader");        
                            $this->setComHeader($com, $e);
                            $timer->stopTimer();


                            // body ersetzen
                            $timer=new timer_com("getBody");        
                            $body             =$this->getBody($com, $e);
                            $timer->stopTimer();
                            
                            //$replaceBodyScript=$this->getComElement($tag, $com['com_path'], "replace_body");

                            $timer=new timer_com("getComElement");        
                            $replaceBodyScript=$this->getComElement($com, "replace_body");
                            $replaceBodyScript=$this->replaceProperty($replaceBodyScript, $e, $com);
                            $timer->stopTimer();
                            
                            
                            if ($replaceBodyScript !== "")
                                {

                                //$php     =file::readTextFile($fileName);
                                //$htmlForm=$e->innertext;

                                //require ($rootDir . $replaceFilename);
                                $timer=new timer_com("eval");
                                eval("?>$replaceBodyScript<?php ");
                                $timer->stopTimer();

                                //file::createTextFile($fileName, $php);
                                //$e->innertext=$htmlForm;

                                }

                            if ($body !== "")
                                {
                                $e->outertext=$body;
                                }

                            $timer=new timer_com("replaceBeginEnd"); 
                            $e->innertext=$this->replaceBeginEnd($com, $e, $e->innertext);
                            $timer->stopTimer();  
                            //$e->outertext=$this->replaceOuterBeginEnd($com['com'], $com['com_path'], $e, $e->outertext);


                            // Query Counter erh?hen, f?r was brauchts da???
                            $this->queryCounter++;

                            //$e->com=null;
                            }
                      //  }
                       
                       
                        
                        
                        
                        
                //    }
        
        
       /* foreach ($this->arrayCom as $com)
            {
            $tag = strtolower($com['com']);

            if (sizeof($tagArray) > 0)
                {
                foreach ($tagArray as $e)
                    {
                    if (isset($e->com))
                        {
                        if (strtolower($e->com) == strtolower($com['com']))
                            {
                                
                               // echo $com['com'];
                                
                            api::checkRquiredApi($com['com']);
                            // muss vor outer replace gel?scht werden
                            $e->com=null;


                            //echo "replace {$e->com}";

                            // falls data_var noch nicht gesetzt wurde
                            if (!isset($e->data_var))
                                $e->data_var=$this->queryCounter;


                            // Header einf?gen
                            // --> Problem doppelte eintr?ge vermeiden  
                            $timer=new timer_com("setComHeader");        
                            $this->setComHeader($com, $e);
                            $timer->stopTimer();


                            // body ersetzen
                            $timer=new timer_com("getBody");        
                            $body             =$this->getBody($com, $e);
                            $timer->stopTimer();
                            
                            //$replaceBodyScript=$this->getComElement($tag, $com['com_path'], "replace_body");

                            $timer=new timer_com("getComElement");        
                            $replaceBodyScript=$this->getComElement($com, "replace_body");
                            $replaceBodyScript=$this->replaceProperty($replaceBodyScript, $e, $com);
                            $timer->stopTimer();
                            
                            
                            if ($replaceBodyScript !== "")
                                {

                                //$php     =file::readTextFile($fileName);
                                //$htmlForm=$e->innertext;

                                //require ($rootDir . $replaceFilename);
                                $timer=new timer_com("eval");
                                eval("?>$replaceBodyScript<?php ");
                                $timer->stopTimer();

                                //file::createTextFile($fileName, $php);
                                //$e->innertext=$htmlForm;

                                }

                            if ($body !== "")
                                {
                                $e->outertext=$body;
                                }

                            $timer=new timer_com("replaceBeginEnd"); 
                            $e->innertext=$this->replaceBeginEnd($com, $e, $e->innertext);
                            $timer->stopTimer();  
                            //$e->outertext=$this->replaceOuterBeginEnd($com['com'], $com['com_path'], $e, $e->outertext);


                            // Query Counter erh?hen, f?r was brauchts da???
                            $this->queryCounter++;

                            //$e->com=null;
                            }
                        }
                    }
                }
            }  */

        return $dom;
        }


    // ersetzt Href Links   (+ form??????????? braucht auch script)
    private function replaceAttribute($dom)
        {
        $domArray=$dom->find($this->htmlElement);

        foreach ($this->arrayComAttribute as $com)
            {
            $tag = strtolower($com['com']);

            foreach ($domArray as $e)
                {
                if (isset($e->$tag))
                    {
                    api::checkRquiredApi($com['com']);

                    // problem bei usergroup, if, if_exist
                    //if ((strtolower($e->$tag) == "on") or (strtolower($e->$tag) == "true")) {

                    //echo "found ".$com['com'];

                    // falls data_var noch nicht gesetzt wurde  --> brauchts das noch???
                    if (!isset($e->data_var))
                        $e->data_var=$this->queryCounter;

                    $this->setComHeader($com, $e);
                    $e->innertext=$this->replaceBeginEnd($com, $e, $e->innertext);


                    // Query Counter erh?hen
                    $this->queryCounter++;
                    //}

                    // l?schen des haupt attribut, gibts irgendwelche probleme??? ja, ist bei replacecomattribute nicht mehr vorhanden (bei div)
                    $e->$tag=null;

                    // l?schen der Attribute
                    // d?rfen erst zuletzt gel?scht werden, da <div table=".." usergroup=".." mehrmals durchlaufen wird
                    /*if (isset($e->table))   
                        $e->table = null;     
                        
                    if (isset($e->menu))   
                        $e->menu = null;     

                    if (isset($e->setfocus))   
                        $e->setfocus = null; */

                    }
                }
            }

        return $dom;
        }


    // ersetzt Komponenten
    //private 
    private function replaceBracket($html)
        {
        foreach ($this->arrayCom as $com)
            {

            //      $tag = $dataCom->data['com'];
            $tag  = $com['com'];

            $regex=new regex(); // abstand wichtig, sonst werden [com] und [com1] verwechselt
            // problem bei nur Platzhalter
            $regex->getRegexTable("(\[" . $com['com'] . " .*?\])", $html);

            if ($regex->count > 0)
                {
                do
                    {

                    //echo "found".$regex->data[0]."************* HTML: $html";
                    $foundCom       =$regex->data[0];
                    $foundComOrginal=$foundCom;

                    // HTMl Zeichen " umwandeln
                    $foundCom       =str_replace("&quot;", "\"", $foundCom);

                    //                $this->setComHeader($com['com'], null, $foundCom);
                    //                $body = $this->getBody($com['com'], null, $com['path'], $foundCom);

                    //$fileName    =$this->createPhpScript($com['com'], null, $foundCom); 


                    //$body = str_ireplace("[php_script]", $fileName, $body);
                    //$this->header = str_ireplace("[php_script]", $fileName, $this->header);      

                    $body           ="no bracket replacement";

                    $html           =str_ireplace($foundComOrginal, $body, $html);

                    //$html = str_ireplace($foundComOrginal, "hier ist ein com", $html);

                    } while ($regex->nextRow());
                }
            } //while ($dataCom->nextRow());                   

        return $html;
        }


    // ersetzt Komponenten
    private function replacePlaceholder($html)
        {


        /*$dataCom = new data();
        $dataCom->getQuery("SELECT * FROM _com");
  
        do {   */

        foreach ($this->arrayCom as $com)
            {
            $tag  = $com['com'];

            $regex=new regex(); // abstand wichtig, sonst werden [com] und [com1] verwechselt
            // problem bei nur Platzhalter
            $regex = regex::getRegexTable("(\[" . $com['com'] . "\])", $html);

            //if ($regex->count > 0)
                //{
                //do
                    foreach ($regex as $data){

                    //echo "found".$regex->data[0]."************* HTML: $html";
                    $foundCom       =$data[0];
                    $foundComOrginal=$foundCom;

                    // HTMl Zeichen " umwandeln
                    $foundCom       =str_replace("&quot;", "\"", $foundCom);

                    $this->setComHeader($com, null, $foundCom);

                    $body=$this->getBody($com, null, $foundCom);


                    // compile component
                    $body=$this->replaceCom($body);
                    //      $body = $this->replaceCom($body);  

                    // replace component
                    $html=str_ireplace($foundComOrginal, $body, $html);

                    //$html = str_ireplace($foundComOrginal, "hier ist ein com", $html);

                    } //while ($regex->nextRow());
                //}
            } //while ($dataCom->nextRow());                   

        return $html;
        }


    // ersetzt Short Bracket's ([user:...] oder [get:...] oder [var:....] oder [error:....])
    // m?gliche Werte f?r $field: short_bracket, php_property oder html_property
    private function replaceShortBracket($html, $field) //$replaceFilename)
        {
        
                                                         // kein schliessende ]
        $regexShortBracket = regex::getRegexTable("(\[([^\]]*?):(.*?)\])", $html); 
           
         foreach ($regexShortBracket as $value)    { 
 
             
             echo "*********************************************************************";
             print_r($value);
// ($this->arrayCom as $com)
            //{
           // $tag  = $com['com'];

            //$regex=new regex(); // abstand wichtig, sonst werden [com] und [com1] verwechselt
            // problem bei nur Platzhalter
            //$regex = regex::getRegexTable("(\[" . $com['com'] . ":(.*?)\])", $html);

            /*if ($regex->count > 0)
                {
                do*/
                
//            foreach ($regex as $value)    {
                    $foundCom=$value[1];
                    $com['com'] = $value[2];

                    // Header einf?gen
                    $this->setComHeader($com, $value[3], $com);

                    //$filename = $dataCom->data['path']."/short_bracket.php";
                    //$filename=$com['path'] . "/$replaceFilename";

                    //echo $filename . "  $foundCom";


                    //$bracket = db::getValue("SELECT $field FROM _com WHERE com='{$com['com']}'"); 
                    //echo "field $com - $field";

                    $bracket=$this->getComElement($com, $field);

                    //echo $bracket;

  //                  echo $value[2];
//die();
                    
                    $bracket=str_ireplace("[property]", $value[3], $bracket);
                    
                    

                    $replace=$this->getComElement($com, "replace");
                    //$replace=$this->replaceProperty($replace, $element, $com);

                    $html   =str_ireplace($foundCom, $bracket, $html);


                    /* if (file::fileExists($filename))
                         {
                         $bracket=file::readTextFile($filename);
                         $bracket=str_ireplace("[property]", $regex->data[1], $bracket);
                         $html   =str_ireplace($foundCom, $bracket, $html);
                         }
                     else
                         {
                         //echo "<p>Error: Bracket file $filename not found.</p>";
                         }  */


                    // echo "html: $html";

                    // $this->setComHeader($com['com'], "")


                    // header setzen???????? f?r welche components

// wieder aktivieren                  } //while ($regex->nextRow());
                //}
  //          } //while ($dataCom->nextRow());  */
             
             
             
             
             
             
             
             
             
             

         
         
         }    
            
            
            
            
            
            
            
            
    //    $file=new file();

        /*  $dataCom=new data();
          $dataCom->getQuery("SELECT * FROM _com");
  
          do
              {   */
      /* wieder aktivieren  foreach ($this->arrayCom as $com)
            {
            $tag  = $com['com'];

            //$regex=new regex(); // abstand wichtig, sonst werden [com] und [com1] verwechselt
            // problem bei nur Platzhalter
            $regex = regex::getRegexTable("(\[" . $com['com'] . ":(.*?)\])", $html);

            /*if ($regex->count > 0)
                {
                do*/
                
   /* wieder aktivieren             foreach ($regex as $value)    {
                    $foundCom=$value[1];

                    // Header einf?gen
                    $this->setComHeader($com, $value[2], $com);

                    //$filename = $dataCom->data['path']."/short_bracket.php";
                    //$filename=$com['path'] . "/$replaceFilename";

                    //echo $filename . "  $foundCom";


                    //$bracket = db::getValue("SELECT $field FROM _com WHERE com='{$com['com']}'"); 
                    //echo "field $com - $field";

                    $bracket=$this->getComElement($com, $field);

                    //echo $bracket;

  //                  echo $value[2];
//die();
                    
                    $bracket=str_ireplace("[property]", $value[2], $bracket);
                    
                    

                    $replace=$this->getComElement($com, "replace");
                    //$replace=$this->replaceProperty($replace, $element, $com);

                    $html   =str_ireplace($foundCom, $bracket, $html);


                    /* if (file::fileExists($filename))
                         {
                         $bracket=file::readTextFile($filename);
                         $bracket=str_ireplace("[property]", $regex->data[1], $bracket);
                         $html   =str_ireplace($foundCom, $bracket, $html);
                         }
                     else
                         {
                         //echo "<p>Error: Bracket file $filename not found.</p>";
                         }  */


                    // echo "html: $html";

                    // $this->setComHeader($com['com'], "")


                    // header setzen???????? f?r welche components

  /* wieder aktivieren                  } //while ($regex->nextRow());
                //}
            } //while ($dataCom->nextRow());  */

        return $html;
        }


    // replace forms replaces with the script.php file
    function replaceForm($dom)
        {
        
            
            
        $domArray=$dom->find('form');


        // --> nur form com
        //foreach ($this->arrayCom as $com)
          //  {
            //$tag = strtolower($dataCom["command"][$n]);
          //  $tag = $com['com'];

            foreach ($domArray as $e)
                {

                //Menu

                $comAction = "";

                if (isset($e->action))
                    $comAction=strtolower(trim($e->action));

                if (isset($e->com_action))
                    $comAction=strtolower(trim($e->com_action));

                if ($comAction !== "")
                    {
                    if ($comAction == strtolower($tag))
                        {
                        $fileName        =$this->createPhpScript($com, $e);


                        // replace script


                        // replace.php (ersetzt

                        $replacePhpScript=$this->getComElement($com, "replace_php_script");
                        $replacePhpScript=$this->replaceProperty($replacePhpScript, $e, $com);

                        if ($replacePhpScript !== "")
                            {
                            $php     =file::readTextFile($fileName);
                            $htmlForm=$e->innertext;

                            /*ini_set('track_errors', '1');
                            //require ($rootDir . $replaceFilename);
                            if (!@eval("?>$replacePhpScript<?php ")) {
                                
                                echo "<p>Error: Fehler bei <b>$tag</b>:<br>$php_errormsg</p>";
                                
                            }*/


                            //echo "$tag<br>";
                            eval("?>$replacePhpScript<?php ");

                            file::createTextFile($fileName, $php);
                            $e->innertext=$htmlForm;

                            //echo "Html 3:".$e->innertext;

                            }


                        /* old code  
                          
                          $replaceFilename="/com/{$com['com']}/replace_php_script.php";
  
                          if (file::fileExists($replaceFilename))
                              {
                              global $rootDir;
  
                              //                    $html        =$e->innertext;
  
                              $php     =file::readTextFile($fileName);
                              $htmlForm=$e->innertext;
  
                              require ($rootDir . $replaceFilename);
                              file::createTextFile($fileName, $php);
                              $e->innertext=$htmlForm;
                              } */


                        // Parameter anh?ngen


                        // PROBLEM URL Parameter
                        $urlParameter="";

                        /*wieder aktivieren
                        $urlParameter = $dataCom["url_parameter"][$n];
                        $urlParameter = $this->replaceComProperty($urlParameter, $e->outertext);
                        */


                        // f?r app modus
                        /*   global $usergrouModus;
                           $teamUrl = "";
                           if ($usergrouModus)
                                                   $teamUrl = "?&team_id=<?php echo \$teamData['id2'];?>";  */


                        // falls Group Modus aktiviert ist, groupid anh?ngen
                        /*  global $groupModus;
                          $urlParameter = "";
                          if ($groupModus) {
                              $urlParameter = "?mygroup=<?php echo \$_REQUEST['mygroup'];?>";    
                              /*$e->innertext = $e->innertext."<input type=\"hidden\" name=\"mygroup\" id=\"mygroup\" value=\"{$_REQUEST['mygroup']}\" />";   */
                        //$url = $url.$urlParameter;
                        // }


                        // erstes Slash abschneiden
                        if ($fileName[0] == "/")
                            $fileName=substr($fileName, 1);

                        $e->action=$this->preIncludePath . $fileName . $urlParameter; //.$teamUrl; //.$hrefOrginal;


                        // hidden input url_redirect hinzuf?gen
                        if (isset($e->redirect))
                            $e->innertext=$e->innertext
                                . "<input type=\"hidden\" name=\"url_redirect\" id=\"url_redirect\" value=\"{$e->redirect}\" />";

                        $e->com_action=null;
                        }
                    }
                }
           // }

        return $dom;
        }


    // ersetzt Href Links   (+ form??????????? braucht auch script)
    function replaceHref($dom)
        {
        foreach ($this->arrayCom as $com)
            {
            $tag = strtolower($com['com']);

            foreach ($dom->find('a')as $e)
                {
                $comHref = "";

                if (isset($e->href))
                    $comHref=strtolower(trim($e->href));

                if (isset($e->com_href))
                    $comHref=strtolower(trim($e->com_href));

                if ($comHref !== "")
                    {
                    if ($comHref == $tag)
                        {
                        $fileName    =$this->createPhpScript($com, $e);
                        $urlParameter=$this->getUrlParameter($com, $e);

                        // erstes Slash abschneiden
                        if ($fileName[0] == "/")
                            $fileName=substr($fileName, 1);

                        $e->href=$this->preIncludePath . $fileName . $urlParameter;
                        }
                    }
                }
            }

        return $dom;
        }


    // ersetzt Komponenten
    private function replaceEvent($dom)
        {

        // $dataCom = sqlQuery("SELECT * FROM _com");

        //for ($n=0; $n<sizeof($dataCom['id']); $n++) {
        //$tag = $dataCom["command"][$n];

        $eventList=array();

        $eventList[]="mouseover";
        $eventList[]="click";
        $eventList[]="dblclick";
        $eventList[]="change";


        //foreach($dom->find("a") as $e) {
        foreach ($dom->find("a," . $this->htmlElement)as $e)
            {
            for ($n=0; $n < sizeof($eventList); $n++)
                {
                if (isset($e->$eventList[$n]))
                    {
                    if ($e->$eventList[$n] !== "")
                        {


                        // ist es ein _html objekt

                        $htmlCode=db::getValue("SELECT html FROM _html WHERE id2='{$e->$eventList[$n]}'");

                        if ($htmlCode !== "")
                            {
                            $htmlCodeId2     =$e->$eventList[$n];

                            //$code            =new code();
                            $compile = new compile();

                            //echo $htmlCode;

                            $htmlCode        =$code->compileText($htmlCode);
                            $htmlCode = $compile->compileText($htmlCode, false);
                            $htmlCodeFilename="/code/html/$htmlCodeId2.php";

                            $htmlCode        ='<?php require "../../snapol/framework.php";?>' . $htmlCode;

                            /* falls javascript
                            $this->header = $this->header.$code->header;
                            
                            $htmlCode = "function  [function_name](){\n ".$htmlCode."\n}";    
                            
                            
                            $htmlCodeFilename = "/code/html/$htmlCodeId2.php"; 
                            $functionName="func_" . rand(0, 10000);
                            $htmlCode      =str_ireplace("[function_name]", $functionName, $htmlCode);

                                      */

                            // php script

                            file::createTextFile($htmlCodeFilename, $htmlCode);


                            //$event            ="on$event";
                            //                    $e->onclick  ="$functionName(); return false;";


                            // Url Parameter anf?gen
                            if (isset($e->url_parameter))
                                {
                                $htmlCodeFilename=$htmlCodeFilename . "?" . $e->url_parameter;
                                }

                            $e->href="$htmlCodeFilename";


                            // script im Header anf?gen
                            //         $this->header=$this->header . '<script src="' . $htmlCodeFilename . '" language="javascript"></script>';

                            }
                        else
                            {
                            //$this->setComHeader($e->$eventList[$n], "/com/" . $e->$eventList[$n], $e, null);
                            $comObject=array();

                            $comObject['com']     =$e->$eventList[$n];
                            $comObject['com_path']="/com/" . $e->$eventList[$n];

                            $this->setComHeader($comObject, $e);

                            //$e                =$this->createJsScript($eventList[$n], "/com/" . $e->$eventList[$n], $e);
                            $e                =$this->createJsScript($eventList[$n], $comObject, $e);

                            $e->$eventList[$n]=null;
                            }


                        // $e->href          ="#";

                        }
                    }
                }


            /*     if(isset($e->click)) { 
                
                echo "found";  
                    $e = $this->createJsScript($eventList[$n], $e);
                    $e->$eventList[$n] = null;
                    }
                
            
                
                
    /*        if(isset($e->mouseover)) {   
                 //echo "found";
                $e = $this->createJsScript("mouseover", $e);
                $e->mouseover = null;
                
            }
            
            
             //Menu
        /*    if(isset($e->click)) {   
             
    // function getComponent
              //  $com = $e->com;
                 
                 //echo "COM: ".$com;
                 $com = $e->click;
                 $e->click = null;
                 
               /*  $outertextOrginal =  $e->outertext;
                 
                 //$comFileName = "/com/$menu/"
                 
             $str = db::getValue("SELECT php FROM _com WHERE com = '$com'");
             $str = $this->replaceComProperty($str, $outertextOrginal);        */


            // Header einf?gen
            // --> Problem doppelte eintr?ge vermeiden
            /*$header = db::getValue("SELECT header FROM _com WHERE command = '$com'");
            $header = $this->replaceComProperty($header, $e->outertext);  
            $this->header = $this->header.$header;   */


            /*        $e->href = "#";
           
                   
               //    $file = new file();
                   
                   
                   //js script
                   $scriptFilename = "/com/$com/script.js";
                   
                   if (file::fileExists($scriptFilename)) {
                       $script = file::readTextFile($scriptFilename);
                       
                       
                       $script = $this->replaceProperty2($script, $e);
                       
                       $functionName = "func_".rand(0,10000)."()";
                       $script = "function $functionName { \r\n $script \r\n }";
                       
                       
                       $fileName = "/code/script/script_".rand(0,10000).".php";     
                       if(isset($e->filename)) { 
                           $fileName = "/code/script/".$e->filename;   // problem, falls standalone modus
                           $e->filename = null;    
                       }   
                   
                       file::createTextFile($fileName, $script);               
                   
                       // script im Header anf?gen
                       $this->header = $this->header.'<script src="'.$fileName.'" language="javascript"></script>';
                       $this->setComHeader($com, $e->outertext); */


            //$script = $this->replaceProperty($script, $outertextOrginal);  
            // Header einf?gen
            // $this->setComHeader($tag,  $regex->data[1]);    

            /*      $e->onclick = "$functionName; return false;";
                  //$script;
              
              } */


            // php script
            /*    $phpScriptFilename = "/com/$com/script.php";  
                
                if ($file->fileExists($phpScriptFilename)) {
                    $script = $file->readTextFile($phpScriptFilename);
                    
                    //echo $script;
                    //echo $outertextOrginal;
                    
                    
                    $script = $this->replaceComProperty($script, $outertextOrginal);    
                    //$e->onclick = $script;
                    $fileName = "/code/script/script_".rand(0,10000).".php";     
                    if(isset($e->filename)) { 
                        $fileName = "/code/script/".$e->filename;   // problem, falls standalone modus
                        $e->filename = null;    
                    }   
                
                
                    $file->createTextFile($fileName, $script);
                
                }    */


            // header
            /*     $headerFilename = "/com/$com/header.html";
                 
                 if ($file->fileExists($headerFilename)) {
                     $header = $file->readTextFile($headerFilename);
                     $header = $this->replaceComProperty($header, $outertextOrginal); 
                     $this->header = $this->header.$header;    
                 
                 }  */


            /* $str = trim($str);
             
             //
             if ($str !== "")
                 $e->outertext = $str;  */

            //  }
            }

        return $dom;
        }


    // setzt den Header f?r einen Komponenten
    private function setComHeader($comObject, $element, $foundCom = "")
        {
        $headerTmp   =$this->getComElement($comObject, "header");
        $headerTmp   =$this->replaceProperty($headerTmp, $element, $comObject); // $foundCom);

        $this->header=$this->header . $headerTmp;

        // js_header.txt auslesen
        $jsHeader    =$this->getComElement($comObject, "js_header");
        $jsHeader    =explode("\n", $jsHeader);

        if (sizeof($jsHeader) > 0)
            {
            for ($n=0; $n < sizeof($jsHeader); $n++)
                {
                $jsHeader[$n] = trim($jsHeader[$n]);

                if ($jsHeader[$n] !== "")
                    {
                    if (!in_array($jsHeader[$n], $this->jsHeader))
                        $this->jsHeader[]=$jsHeader[$n];
                    }
                }
            }


        // css_header.txt auslesen
        $jsHeader=$this->getComElement($comObject, "css_header");
        $jsHeader=explode("\n", $jsHeader);

        if (sizeof($jsHeader) > 0)
            {
            for ($n=0; $n < sizeof($jsHeader); $n++)
                {
                $jsHeader[$n] = trim($jsHeader[$n]);

                if ($jsHeader[$n] !== "")
                    {
                    if (!in_array($jsHeader[$n], $this->cssHeader))
                        $this->cssHeader[]=$jsHeader[$n];
                    }
                }
            }
        }


    // read body file of a component       //$foundCom
    private function getBody($comObject, $element, $foundCom = "")
        {
        if (file::fileExists($comObject['com_path'] . "/function.php"))
            {
            //if (file::fileExists($comPath."/function.js")) {
            //$script      =$this->getComElement($com, $comPath, "js_script");
            //$body=$this->getComElement($com, $comPath, "js_function");  
            $body=$this->getComElement($comObject, "php_function");

            if (file::fileExists($comObject['com_path'] . "/script.php"))
                {
                $phpScript=$this->getComElement($comObject, "php_script");

                if ($phpScript !== "")
                    $phpFilename=$this->createPhpScript($comObject, $element);


                // falls ein php script aufgerufen wird
                $body=str_ireplace("[php_script]", $phpFilename, $body);
                }
            }
        else
            {
            $body=$this->getComElement($comObject, "body");
            }

        $body=$this->replaceProperty($body, $element, $comObject); //$foundCom);

        $body=trim($body);
        return $body;
        }

    function replaceBeginEnd($comObject, $element, $html)
        {
        echo "<p>*********</p>";
        $timer=new timer_com("begin");        
        $begin=$this->getComElement($comObject, "begin");
        $timer->stopTimer();
        
        $timer=new timer_com("replace property"); 
        $begin=$this->replaceProperty($begin, $element, $comObject);
        $timer->stopTimer();
        
        $timer=new timer_com("replace begin"); 
        $replaceBegin=$this->getComElement($comObject, "replace_begin");
        
        
        $replaceBegin=$this->replaceProperty($replaceBegin, $element, $comObject);
        $timer->stopTimer();
        
        if ($replaceBegin !== "") {
            $timer=new timer_com("eval replace begin");
          /*  eval("?>$replaceBegin<?php ");*/
            $timer->stopTimer();      
        }
        
        
        //$end = db::getValue("SELECT end FROM _com WHERE com='$com'");     
        $timer=new timer_com("end");
        $end    =$this->getComElement($comObject, "end");
        $end    =$this->replaceProperty($end, $element, $comObject);
        $timer->stopTimer();
        
        $e      =$element;

        //$replace = db::getValue("SELECT `replace` FROM _com WHERE com='$com'");    
        $replace=$this->getComElement($comObject, "replace");


        //echo "$com<br>";  
        $timer=new timer_com("eval replace");  
        eval("?>$replace<?php ");
        $timer->stopTimer();
        
        $html=$begin . $html . $end;


        // Outer begin

        echo "<p>*********</p>";
        return $html;
        }

    function replaceOuterBeginEnd($comObject, $element, $html) {
    /*     $begin=$this->getComElement($com, $comPath, "outer_begin");
         $begin=$this->replaceProperty($begin, $element);
         $replaceBegin=$this->getComElement($com, $comPath, "replace_outer_begin");
 
         if ($replaceBegin !== "")
             eval ("?>$replaceBegin<?php ");
 
         //$end = db::getValue("SELECT end FROM _com WHERE com='$com'");     
         $end    =$this->getComElement($com, $comPath, "outer_end");
         $end    =$this->replaceProperty($end, $element);
 
         $e      =$element;
 
         //$replace = db::getValue("SELECT `replace` FROM _com WHERE com='$com'");    
         $replace=$this->getComElement($com, $comPath, "replace");
 
         //echo "$com<br>";
 
         eval ("?>$replace<?php ");
 
         $html   =$begin . $html . $end; */


    // Outer begin

    return $html; }


    // gibts es ein begin.php und end.php File
    /*  $beginFilename="/com/{$com['com']}/begin.php";

      if (file::fileExists($beginFilename))
          {
          $html           =file::readTextFile($beginFilename);
          //                        $tmp         =$this->replaceProperty($tmp, $e->outertext);
          $html           =$this->replaceProperty($html, $e);


          // replace.php (ersetzt
          $replaceFilename="/com/{$com['com']}/replace_begin.php";

          if (file::fileExists($replaceFilename))
              {
              //echo " $html  found";
              global $rootDir;
              //$html        =$e->innertext;
              require($rootDir . $replaceFilename);
              //echo $html;
              //$e->innertext=$html;
              }

          $e->innertext=$html . $e->innertext;
          }

      $endFilename="/com/{$com['com']}/end.php";

      if (file::fileExists($endFilename))
          {
          $tmp         =file::readTextFile($endFilename);
          //$tmp         =$this->replaceProperty($tmp, $e->outertext);
          $tmp         =$this->replaceProperty($tmp, $e);
          $e->innertext=$e->innertext . $tmp;
          }


      //echo "found $tag - $n,";    
      // OUTER REPLACE 
      // gibts es ein outer_begin.php und outer_end.php File
      $beginFilename="/com/{$com['com']}/outer_begin.php";

      if (file::fileExists($beginFilename))
          {
          $tmp         =file::readTextFile($beginFilename);
          //$tmp         =$this->replaceProperty($tmp, $e->outertext);
          $tmp         =$this->replaceProperty($tmp, $e);


          //echo $com['com'];
          $e->outertext=$tmp . $e->outertext;
          }


      // replace.php (ersetzt
      $replaceFilename="/com/{$com['com']}/replace.php";

      if (file::fileExists($replaceFilename))
          {
          global $rootDir;
          $html=$e->innertext;
          require($rootDir . $replaceFilename);
          $e->innertext=$html;
          }  */


    // setzt den Header f?r einen Komponenten
    //private function getComUrlParameter($com, $foundCom)
    private function getUrlParameter($comObject, $element, $foundCom = "")
        {
        //$file                =new file();
        //$urlParameterFilename="/com/$com/url_parameter.txt";
        //      if (!$file->fileExists($bodyFilename)) 
        //       $bodyFilename = "/com/$com/body.php";

        // echo $bodyFilename;
        $urlParameter="";
        //$urlParameter = db::getValue("SELECT url_parameter FROM _com WHERE com='$com'");   
        $urlParameter=$this->getComElement($comObject, "url_parameter");

        $urlParameter=$this->replaceProperty($urlParameter, $element, $comObject); // $foundCom, false);

        // falls group modus, mygroup anh?ngen
        global $groupModus;

        if ($groupModus)
            {
            $urlParameter=$urlParameter . "&mygroup=<?php echo \$_REQUEST['mygroup']; ?>";

            //$data['_mygroup'] = $mygroup;
            }


        /* if (file::fileExists($urlParameterFilename))
             {
             $urlParameter=file::readTextFile($urlParameterFilename);
 
             //echo $urlParameter;
             //$urlParameter=$this->replaceProperty($urlParameter, $foundCom, false);
             $body = db::getValue("SELECT body FROM _com WHERE com='$com'");   
             $urlParameter=$this->replaceProperty($urlParameter, $element, $foundCom, false);
             // echo $urlParameter;       
 
             // falls group modus, mygroup anh?ngen
             global $groupModus;
 
             if ($groupModus)
                 {
                 $urlParameter=$urlParameter . "&mygroup=<?php echo \$_REQUEST['mygroup']; ?>";
 
                 //$data['_mygroup'] = $mygroup;
                 }
             //}
 
             }*/

        return $urlParameter;
        }


    // erstellt das com script (script.php)
    //private function createComPhpScript($com, $filename, $outertext)
    private function createPhpScript($comObject, $element, $foundCom = "")
        {


        //$script = db::getValue("SELECT php_script FROM _com WHERE com='$com'");   
        $script=$this->getComElement($comObject, "php_script");
        $script=$this->replaceProperty($script, $element, $comObject); // $foundCom);


        // Filename des Script auslesen
        //        $filename="/code/script/script_" . rand(0, 10000) . ".php";
        $this->phpScriptCounter++;
        $filename="/code/php_script/" . $this->compileTable . "_" . $this->compileField . "_" . $this->compileId . "_"
            . $this->phpScriptCounter . ".php";


        // sollte in script_filename ge?ndert werden
        if (isset($element->script_filename))
            {
            $filename                ="/code/php_script/"
                . $element->script_filename; // problem, falls standalone modus
            $element->script_filename=null;
            }


        // falls kein Filename ?bergeben wurde
        /*   if ($filename == "")
               $filename="/code/script/script_" . rand(0, 10000) . ".php"; */


        /*  if(isset($e->filename)) { 
              $filename = "/code/script/".$e->filename;   // problem, falls standalone modus
              //$e->filename = null;    
          } */


        // echo "createt file $filename";
        file::createTextFile($filename, $script);


        // php script
        /*   $file             =new file();
           $phpScriptFilename="/com/$com/script.php";
   
           if ($file->fileExists($phpScriptFilename))
               {
               $script  =$file->readTextFile($phpScriptFilename);
   
               //            $script=$this->replaceProperty($script, $outertext);
               $script  =$this->replaceProperty($script, $element, $foundCom);
   
               //$e->onclick = $script;
   
   
               // Filename des Script auslesen
               $filename="/code/script/script_" . rand(0, 10000) . ".php";
   
               // sollte in script_filename ge?ndert werden
               if (isset($element->filename))
                   {
                   $filename         ="/code/script/" . $element->filename; // problem, falls standalone modus
                   $element->filename=null;
                   }
   
   
               // falls kein Filename ?bergeben wurde
               /*   if ($filename == "")
                      $filename="/code/script/script_" . rand(0, 10000) . ".php"; */


        /*  if(isset($e->filename)) { 
              $filename = "/code/script/".$e->filename;   // problem, falls standalone modus
              //$e->filename = null;    
          } */


        // echo "createt file $filename";
        /*      $file->createTextFile($filename, $script);
              }
          else
              {
  
              // echo "kein script.php file  vorhanden $phpScriptFilename";
              $filename="";
              }  */

        return $filename;
        }

    private function createJsScript($event, $comObject, $element)
        { //} $filename, $outertext) {
        //$com      =$element->$event;

        //echo "com: $com";

        // ist ein PHP Script vorhanden
        $phpFilename="";
        $phpScript=$this->getComElement($comObject, "php_script");

        if ($phpScript !== "")
            $phpFilename=$this->createPhpScript($comObject, $element);


        // ist ein php script file vorhanden
        /*   $phpScriptFilename="/com/$com/script.php";
           $codePhpScriptFilename="";
   
           if (file::fileExists($phpScriptFilename))
               {
               $codePhpScriptFilename=$this->createPhpScript($com, $element);
               }
   
           $scriptFilename="/com/$com/script.js";
   
           if (file::fileExists($scriptFilename))
               {
               $script      =file::readTextFile($scriptFilename);*/


        //$script = db::getValue("SELECT js_script FROM _com WHERE com='$com'");  

        if (file::fileExists($comObject['com_path'] . "/script.js"))
            {
            $script=$this->getComElement($comObject, "js_script");
            }
        else
            {
            $script=$this->getComElement($comObject, "js_function");
            $script="function  [function_name](){ " . $script . "}";
            }


        //$script  =$this->replaceProperty($script, $element, $foundCom);


        // falls ein php script aufgerufen wird
        $script      =str_ireplace("[php_script]", $phpFilename, $script);
        // --------> WIEDER AKTIVIEREN

        $script      =$this->replaceProperty($script, $element, $comObject);

        $functionName="func_" . rand(0, 10000);
        //$script = "function $functionName() { \r\n $script \r\n }";

        $script      =str_ireplace("[function_name]", $functionName, $script);

        //$fileName    ="/code/script/script_" . rand(0, 10000) . ".php";

        /*if (isset($element->filename))
            {
            $fileName   ="/code/script/" . $element->filename; // problem, falls standalone modus
            $e->filename=null;
            }  */

        //file::createTextFile($fileName, $script);

        // Dateiendung muss php sein, da sonst Php Komponenten nicht ausgef?hrt werden k?nnen
        $this->jsScriptCounter++;
        $filename="/code/js_script/" . $this->compileTable . "_" . $this->compileField . "_" . $this->compileId . "_"
            . $this->jsScriptCounter . ".php";

        // sollte in script_filename ge?ndert werden
        if (isset($element->script_filename))
            {
            $filename                ="/code/js_script/" . $element->script_filename; // problem, falls standalone modus
            $element->script_filename=null;
            }

        file::createTextFile($filename, $script);


        // script im Header anf?gen
        $this->header=$this->header . '<script src="' . $filename . '" language="javascript"></script>';
        $this->setComHeader($comObject, $element->outertext);


        //return "$functionName; return false;";
        /*        }
            else
                {
                echo "Error: Kein Event File f?r $com gefunden";
                } */

        /*  $functionParameterFilename="/com/$com/function_parameter.txt";
          $functionParameter="";
  
          if (file::fileExists($functionParameterFilename))
              {
              $functionParameter=file::readTextFile($functionParameterFilename);
  
              //echo $functionParameter;
  
              $functionParameter=$this->replaceProperty($functionParameter, $element);
  
              //echo "************".$functionParameter;
  
              } */

        $functionParameter=$this->getComElement($comObject, "function_parameter");
        $functionParameter=$this->replaceProperty($functionParameter, $element, $comObject);


        // Java Script spezifisches on anf?gen
        $event            ="on$event";
        $element->$event  ="$functionName($functionParameter); return false;";
        return $element;

        //$script = $this->replaceProperty($script, $outertextOrginal);  
        // Header einf?gen
        // $this->setComHeader($tag,  $regex->data[1]);    


        //$script;

        }

    private function replaceProperty($html, $htmlElement, $comObject)
        {


        // query counter ersetzen, falls attribut data_var vorhanden, sonst z?hler
        if (isset($htmlElement->data_var))
            {
            $html=str_ireplace("[query_counter]", $htmlElement->data_var, $html);
            }

        $timer = new timer_com("get Regex Html"); 
        $phpData=re::getRegexArray("\[html_property:(.*?)\]", $html);
        $timer->stopTimer();

        foreach ($phpData as $parameterKey)
            {
            $parameterValue = $htmlElement->$parameterKey;
            $timer = new timer_com("replace Short Bracket Html Property");
            $parameterValue =$this->replaceShortBracket($parameterValue, "html_property");
            $timer->stopTimer();
            
            $html           =str_ireplace("[html_property:$parameterKey]", $parameterValue, $html);
            }

        $timer = new timer_com("get Regex Php"); 
        $phpData=re::getRegexArray("\[php_property:(.*?)\]", $html);
        $timer->stopTimer();

        foreach ($phpData as $parameterKey)
            {
            $parameterValue = $htmlElement->$parameterKey;
            $timer = new timer_com("replace Short Bracket Php Property"); 
            $parameterValue =$this->replaceShortBracket($parameterValue, "php_property");
            $timer->stopTimer();
            
            //echo $parameterValue;
            
               $parameterValue = str_replace('<?php echo ', '{', $parameterValue);
      $parameterValue = str_replace(';?>', '}', $parameterValue);
      $parameterValue = str_replace('; ?>', '}', $parameterValue);
            
            
            $html           =str_ireplace("[php_property:$parameterKey]", $parameterValue, $html);
            }

        // zus?tzliche Php Files
        $phpData=re::getRegexArray("\[php:(.*?)\]", $html);

        foreach ($phpData as $value)
            {
            //echo $value;
            //print_r($comObject);

            if ($this->developerModus)
                {
                $filename=$comObject['com_path'] . "/php/$value";
                $script  =file::readTextFile($filename);
                }
            else
                {
                $comId =db::getValue("SELECT id FROM _com WHERE com='{$comObject['com']}'");
                $script=db::getValue("SELECT script FROM _com_script WHERE __com=$comId AND filename='{$value}'");
                }

            $script=$this->replaceProperty($script, $htmlElement, $comObject);

            $this->phpScriptCounter++;
            $filename
                ="/code/php_script/" . $this->compileTable . "_" . $this->compileField . "_" . $this->compileId . "_"
                . $this->phpScriptCounter . ".php";

            file::createTextFile($filename, $script);

            $html=str_ireplace("[php:$value]", $filename, $html);
            }

        $html=trim($html);

        return $html;
        }


    // ersetzt die Eigenschaften eines Komponenten
    private function replacePropertyOld($str, $element, $foundCom = "", $cleanCode = true)
        {


        // query counter ersetzen, falls attribut data_var vorhanden, sonst z?hler
        if (isset($element->data_var))
            {
            $str=str_ireplace("[query_counter]", $element->data_var, $str);
            }

        /*else {
            $str = str_ireplace("[query_counter]", $this->queryCounter, $str);
            if (isset($element->table))
                $element->data_var = $this->queryCounter;
        } */

        $reTmp        =new re();

        $dataParameter=reGetTable("\[html_property:(.*?)\]", $str);

        for ($m=0; $m < sizeof($dataParameter); $m++)
            {
            $parameterKey  = $dataParameter[$m][1];
            //$parameterValue  =reGetValue($parameterKey, $outertext);
            //            $parameterValue=re::getAttributeValue($parameterKey, $outertext);
            $parameterValue=$element->$parameterKey;

            //echo "key=$parameterKey --> $parameterValue";

            //re::getAttributeValue($parameterKey, $outertext);

            // falls foundCom ?bergeben wurde
            if ($foundCom !== "")
                {
                //echo "parameter value!!!!!!!!!!!!!!";
                $parameterValue=re::getAttributeValue($parameterKey, $foundCom);
                }


            // Inhalt ersetzen (--> nur ShortBracks sind erlaubt)
            /*  $parameterValue = $this->compileVariable($parameterValue);*/
            $parameterValue=$this->replaceShortBracket($parameterValue, "html_property");

            //echo "//////// $str";
            $str           =str_ireplace("[html_property:$parameterKey]", $parameterValue, $str);

            //echo "//////// $str";

            }


        // Parameter auslesen
        $dataParameter=reGetTable("\[php_property:(.*?)\]", $str);
        //print_r($dataParameter);   
        //echo $outertext;           

        for ($m=0; $m < sizeof($dataParameter); $m++)
            {
            $parameterKey = $dataParameter[$m][1];
            //$parameterValue  =reGetValue($parameterKey, $outertext);
            //$parameterValue=re::getAttributeValue($parameterKey, $outertext);

            try
                {

                // PROBLEM BEI BRACKET REPLACE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! da dann kein element mitgeliefert wird
                $parameterValue=$element->$parameterKey;
                }
            catch( Exception $e )
                {
                echo "<p>Exception abgefangen: {$e->getMessage()} bei Parameter Key $parameterKey </p>";
                }


            // falls foundCom ?bergeben wurde
            if ($foundCom !== "")
                {
                $parameterValue=re::getAttributeValue($parameterKey, $foundCom);
                }


            // Inhalt ersetzen
            /*  $parameterValue = $this->compileVariable($parameterValue);*/
            $parameterValue=$this->replaceShortBracket($parameterValue, "php_property");


            //$parameterValue= $this->replaceShortBracket()


            // z.B. von table oder sql attribute entfernen
            $parameterValue=$this->cleanCode($parameterValue);


            // falls kein clean code durchgef?hrt wird, gibts probleme bei <createtable table="[php_property:table]"></createtable>
            /*if ($cleanCode)
            $parameterValue = $this->cleanCode($parameterValue);  */


            //echo "parameter key $parameterKey = $parameterValue ***";

            // clean code: kann das zu probleme f?hren (es muss z.B. f?r <sqlexec sql="insert into ...[detail:..] .." entfernt werden)
            //$parameterValue = $this->compileText($parameterValue);
            //$parameterValue = $this->cleanCode($parameterValue);


            //echo "key $parameterKey value $parameterValue aus $parameter";

            $str           =str_ireplace("[php_property:$parameterKey]", $parameterValue, $str);
            //$header=str_ireplace("[php_property:$parameterKey]", $parameterValue, $header);  
            //$componentHeader =str_ireplace("[php_property:$parameterKey]", $parameterValue, $componentHeader);
            }


        // ersetzen von Php und Js Scripte

        $dataPhpScript=re::getRegexArray("\[php:(.*?)\]", $str);
        //reGetTable("\[php_property:(.*?)\]", $str); 

        foreach ($dataPhpScript as $value)
            {
            echo $value;
            //$this->createPhpScript()

            //     file::readTextFile("")


            //$script=$this->replaceProperty($script, $element, $foundCom);

            }

        /*$str=str_ireplace("[webdir]", "", $str);
        //$header=str_ireplace("[webdir]", "", $header);  
        $str=str_ireplace("[com_path]", $dataCom["path"][$n], $str); 
        $str=str_ireplace("[com_path2]", $dataCom["path"][$n], $str); */
        //$header=str_ireplace("[com_path]", $dataCom["path"][$n], $header);  

        $str=trim($str);

        return $str;
        }


    // ersetzt die Eigenschaften eines Komponenten
    /*   private function replaceProperty($str, $outertext, $cleanCode = true)
           {
           // $html
   
           // To Do
           // falls kein parameter im text vorhanden ist!!!!!!!!!!!!!!!!!!
   
           $reTmp        =new re();
   
           $dataParameter=reGetTable("\[html_property:(.*?)\]", $str);
   
           for ($m=0; $m < sizeof($dataParameter); $m++)
               {
               $parameterKey  = $dataParameter[$m][1];
               //$parameterValue  =reGetValue($parameterKey, $outertext);
               $parameterValue=re::getAttributeValue($parameterKey, $outertext);
   
               // Inhalt ersetzen
               /*  $parameterValue = $this->compileVariable($parameterValue);*/
    /*           $parameterValue=$this->replaceShortBracket($parameterValue, "html_property.php");
   
               $str           =str_ireplace("[html_property:$parameterKey]", $parameterValue, $str);
               }
   
   
           // Parameter auslesen
           $dataParameter=reGetTable("\[php_property:(.*?)\]", $str);
           //print_r($dataParameter);   
           //echo $outertext;           
   
           for ($m=0; $m < sizeof($dataParameter); $m++)
               {
               $parameterKey  = $dataParameter[$m][1];
               //$parameterValue  =reGetValue($parameterKey, $outertext);
               $parameterValue=re::getAttributeValue($parameterKey, $outertext);
               
               // z.B. von table oder sql attribute entfernen
               $parameterValue = $this->cleanCode($parameterValue);
               
               
               
   
               // Inhalt ersetzen
               /*  $parameterValue = $this->compileVariable($parameterValue);*/
    /*         $parameterValue=$this->replaceShortBracket($parameterValue, "php_property.php");
 
 
             // falls kein clean code durchgef?hrt wird, gibts probleme bei <createtable table="[php_property:table]"></createtable>
             /*if ($cleanCode)
             $parameterValue = $this->cleanCode($parameterValue);  */


    //echo "parameter key $parameterKey = $parameterValue ***";

    // clean code: kann das zu probleme f?hren (es muss z.B. f?r <sqlexec sql="insert into ...[detail:..] .." entfernt werden)
    //$parameterValue = $this->compileText($parameterValue);
    //$parameterValue = $this->cleanCode($parameterValue);


    //echo "key $parameterKey value $parameterValue aus $parameter";

    /*          $str           =str_ireplace("[php_property:$parameterKey]", $parameterValue, $str);
              //$header=str_ireplace("[php_property:$parameterKey]", $parameterValue, $header);  
              //$componentHeader =str_ireplace("[php_property:$parameterKey]", $parameterValue, $componentHeader);
              }
  
  
          /*$str=str_ireplace("[webdir]", "", $str);
          //$header=str_ireplace("[webdir]", "", $header);  
          $str=str_ireplace("[com_path]", $dataCom["path"][$n], $str); 
          $str=str_ireplace("[com_path2]", $dataCom["path"][$n], $str); */
    //$header=str_ireplace("[com_path]", $dataCom["path"][$n], $header);  

    /*      $str=trim($str);
  
          return $str;
          }    */

    private function cleanCode($text)
        {
        $text=str_replace('<?php echo ', '{', $text);

        $text=str_replace(';?>', '}', $text);

        $text=str_replace('; ?>', '}', $text);

        return $text;
        }


    // innerhalb von Variablen
    function compileVariable($html)
        {


        // replace variable
        /*  $html=preg_replace("/\[var:(.*?)\]/is", '<?php echo \$$1;?>', $html);   
         
           
              // replace session object
          $html=preg_replace("/\[session:(.*?)\]/is", '<?php echo \$_SESSION[\'$1\'];?>', $html);   
         // vor replacedata 
           
           
         //  $html = $this->replaceUserData($html);
           $html = $this->replaceCmsData($html);     
           
           
               // replace detail fields --> wird m?hsam f?r clean code
      
      /*     $html=preg_replace("/\[detail:(.*?)\]/is", '<?php echo $lineform[\'$1\'];?>', $html); */

        // query objekt einf?gen


        //echo $html;
        $html=$this->replaceParameter($html);
        $html=$this->replaceComPlaceholder($html);

        $html=$this->replaceComShortBracket($html);


        // replace variable
        /*   $html=preg_replace("/\[var:(.*?)\]/is", '<?php echo \$$1;?>', $html);   
          
          
           
           // replace session object
           $html=preg_replace("/\[session:(.*?)\]/is", '<?php echo \$_SESSION[\'$1\'];?>', $html);   
          
           
           
           // replace cookie object
           $html=preg_replace("/\[cookie:(.*?)\]/is", '<?php echo \$_COOKIE[\'$1\'];?>', $html);  */


        // replace placeholder ([http_useragent], [host] etc.)
        /*      $dataCom = sqlQuery("SELECT * FROM _com");
              
              
              // muss ersetzt werden
              for ($n=0; $n<sizeof($dataCom['id']); $n++) {
                   $html = str_ireplace("[".$dataCom["com"][$n]."]", $dataCom["php"][$n], $html);
              }  */

        return $html;
        }


    // muss anders heissen --> verwirrung mit html element
    function getComElement2($comObject, $element)
        {
        if ($this->developerModus)
            {
            $comPath =$comObject['com_path'];

            $filename="$comPath/$element.php";

            if ($element == "url_parameter")
                $filename="$comPath/url_parameter.txt";

            if ($element == "function_parameter")
                $filename="$comPath/function_parameter.txt";

            if ($element == "js_header")
                $filename="$comPath/js_header.txt";

            if ($element == "css_header")
                $filename="$comPath/css_header.txt";

            if ($element == "php_script")
                $filename="$comPath/script.php";

            if ($element == "js_script")
                $filename="$comPath/script.js";

            if ($element == "js_function")
                $filename="$comPath/function.js";

            if ($element == "php_function")
                $filename="$comPath/body.php";


            //echo "<p>not found $filename</p>";               

            $html="";

            if (file::fileExists($filename))
                {
                $html=file::readTextFile($filename);
                }
            else
                {
                //echo "<p>not found $filename</p>";               

                }
            }
        else
            {
            $html=db::getValue("SELECT `$element` FROM _com WHERE com='$com'");
            }

        return $html;
        }


    // muss anders heissen --> verwirrung mit html element
    function getComElement($comObject, $element)
        {
        if ($this->developerModus)
            {
            $filename="{$comObject['com_path']}/$element.php";

            if ($element == "url_parameter")
                $filename="{$comObject['com_path']}/url_parameter.txt";

            if ($element == "function_parameter")
                $filename="{$comObject['com_path']}/function_parameter.txt";

            if ($element == "js_header")
                $filename="{$comObject['com_path']}/js_header.txt";

            if ($element == "css_header")
                $filename="{$comObject['com_path']}/css_header.txt";

            if ($element == "php_script")
                $filename="{$comObject['com_path']}/script.php";

            if ($element == "js_script")
                $filename="{$comObject['com_path']}/script.js";

            if ($element == "js_function")
                $filename="{$comObject['com_path']}/function.js";

            if ($element == "php_function")
                $filename="{$comObject['com_path']}/body.php";


            //echo "<p>not found $filename</p>";               

            $html="";

            if (file::fileExists($filename))
                {
                $html=file::readTextFile($filename);
                }
            else
                {
                //echo "<p>not found $filename</p>";               

                }
            }
        else
            {
            $html=db::getValue("SELECT `$element` FROM _com WHERE com='{$comObject['com']}'");
            }

        return $html;
        }

    function replaceMailAdress($buffer)
        {

        /*
        <script language="JavaScript" type="text/JavaScript">
          a="urs.lang";
          b="gmail.com";
          document.write("<a href=\"mailto:"+a+"@"+b+"\">"+a+"@"+b+"</a>");
        </script>*/


        // --> ersetzt nur Best Practice

        $re="/<a href=\"mailto:(.*?)\">(.*?)<\/a>/is";
        preg_match_all($re, $buffer, $out2, PREG_SET_ORDER);

        for ($n=0; $n < sizeof($out2); $n++)
            {
            $inhaltReplace = $out2[$n][0];
            $mailAdress    =trim($out2[$n][1]);
            $mailName      =trim($out2[$n][2]);


            // nur ersetzen, falls $mailAdress eine g?ltige Mail Adresse ist (sonst gibt es probleme bei datenfelder)

            if (check::checkEmail($mailAdress))
                {
                $mail      =explode("@", $mailAdress);

                //print_r($mail);


                // falls mailName der eMail Adresse entspricht
                //if ($mailAdress == $mailName)
                $mailName  ="\"+a+\"@\"+b+\"";


                // ?bernahmen von class id etc. 

                $javaScript="<script language=\"JavaScript\" type=\"text/JavaScript\">a=\"" . $mail[0] . "\";b=\""
                    . $mail[1] . "\";document.write(\"<a href=mailto:\"+a+\"@\"+b+\">" . $mailName
                    . "</a>\");</script>";

                //$buffer = "email".$javaScript;

                $buffer    =str_replace($inhaltReplace, $javaScript, $buffer);
                }
            else
                {

                // falls keine g?ltige eMail Adresse, vermutlich ist es dann tabellen feld

                $mailAdress=$this->cleanCode($mailAdress);

                $mailAdress=str_replace("{", "", $mailAdress);
                $mailAdress=str_replace("}", "", $mailAdress);

                $phpCode   ='<?php $mail=explode("@", ' . $mailAdress . ');if (sizeof($mail)>1) { 
          echo "<script language=\"JavaScript\" type=\"text/JavaScript\">a=\"" . $mail[0] . "\";b=\"" . $mail[1]
            . "\";document.write(\"<a href=mailto:\"+a+\"@\"+b+\">\"+a+\"@\"+b+\"</a>\");</script>";} else { ?>'
                    . $inhaltReplace . '<?php }?>';

                //   echo $mailAdress;
                $buffer    =str_replace($inhaltReplace, $phpCode, $buffer);
                }
            }

        return $buffer;
        }
    }
?>
